package com.wendy.common.core.page;

import com.wendy.common.core.utils.StringUtils;
import com.wendy.common.core.utils.sql.SqlUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PageBO implements Serializable {

    private static final long serialVersionUID = 5754782319981048853L;



    /**
     * 分页参数合理化
     */
    private Boolean reasonable = true;
    /**
     * 当前记录起始索引
     */
    private Integer pageNum;

    /**
     * 每页显示记录数
     */
    private Integer pageSize;

    /**
     * 排序列
     */
    private String orderByColumn;


    private String isAsc;

    public List<OrderByItem> orderByItems;



    public PageBO() {

    }

    public PageBO(Boolean reasonable, Integer pageNum, Integer pageSize, String orderByColumn, String isAsc) {
        this.reasonable = reasonable;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.orderByColumn = orderByColumn;
        this.isAsc = isAsc;
        this.orderByItems = buildOrderItem();
    }

    public List<OrderByItem> getOrderItems() {
        return orderByItems;
    }

    public void setOrderItems(List<OrderByItem> orderByItems) {
        this.orderByItems = orderByItems;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn() {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public Boolean getReasonable() {
        return reasonable;
    }

    public void setReasonable(Boolean reasonable) {
        this.reasonable = reasonable;
    }

    public String getIsAsc() {
        return isAsc;
    }

    public void setIsAsc(String isAsc) {
        this.isAsc = isAsc;
    }
    /**
     * 构建排序
     *
     * 支持的用法如下:
     * {isAsc:"asc",orderByColumn:"id"} order by id asc
     * {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
     * {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc
     * {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc
     */
    private List<OrderByItem> buildOrderItem() {
        if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
            return new ArrayList<>();
        }
        String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
        orderBy = StringUtils.toUnderScoreCase(orderBy);
        // 兼容前端排序类型
        isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});

        String[] orderByArr = orderBy.split(StringUtils.COMMA);
        String[] isAscArr = isAsc.split(StringUtils.COMMA);
        if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
            return new ArrayList<>();
        }

        List<OrderByItem> list = new ArrayList<>();
        // 每个字段各自排序
        for (int i = 0; i < orderByArr.length; i++) {
            String orderByStr = orderByArr[i];
            String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
            if ("asc".equals(isAscStr)) {
                list.add(OrderByItem.asc(orderByStr));
            } else if ("desc".equals(isAscStr)) {
                list.add(OrderByItem.desc(orderByStr));
            }
        }
        return list;
    }

    @Override
    public String toString() {
        return "PageBO{" +
                "reasonable=" + reasonable +
                ", pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", orderByColumn='" + orderByColumn + '\'' +
                ", isAsc='" + isAsc + '\'' +
                '}';
    }
}
